package net.tooeye.easymes.components;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import net.tooeye.easyplus.R;

import java.security.PublicKey;

/*
  自定义的的上拉加载组件
 */

public class ReLoadListView extends ListView  implements AbsListView.OnScrollListener {


    View footer;
    int totalItemCount;
    int lastVisibleItem;
    boolean isLoading;
    ILoadListener iLoadListener;

    public ReLoadListView(Context context) {
        super(context);
        initView(context);
    }


    public ReLoadListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ReLoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //三种构造方法
        initView(context);
    }

    //添加布局
    private  void initView(Context context){
        LayoutInflater inflater =  LayoutInflater.from(context);
        footer = inflater.inflate(R.layout.listview_footview,null);
        footer.findViewById(R.id.load_layout).setVisibility(View.GONE);
        this.addFooterView(footer);
        this.setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (totalItemCount == lastVisibleItem && scrollState == SCROLL_STATE_IDLE){
            footer.findViewById(R.id.load_layout).setVisibility(View.VISIBLE);
            if(!isLoading){
                isLoading = true;
                footer.findViewById(R.id.load_layout).setVisibility(View.VISIBLE);
                this.iLoadListener.onLoad();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.lastVisibleItem = firstVisibleItem + visibleItemCount;
        this.totalItemCount = totalItemCount;

    }

    public void setInterface(ILoadListener iLoadListener){
        this.iLoadListener = iLoadListener;
    }

    public void  loadComplete(){
        footer.findViewById(R.id.load_layout).setVisibility(View.GONE);
        isLoading = false;
    }
    public interface  ILoadListener{
        public void onLoad();
    }

}
